package com.example.coinhelper.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.coinhelper.Bean.Type;
import com.example.coinhelper.R;

import java.util.List;

public class TypeAdapter extends BaseAdapter {
    private Context mContext;
    private List<Type> mTypeList;
    private int selectPos = 0; // 表示选中的位置
    public TypeAdapter(Context context, List<Type> typeList) {
        this.mContext = context;
        this.mTypeList = typeList;
        // 其他初始化操作...
    }
    public int getSelectPos(){
        return this.selectPos;
    }
    public void setSelectPos(int selectPos){
        this.selectPos = selectPos;
    }
    @Override
    public int getCount() {
        return mTypeList.size();
    }

    @Override
    public Object getItem(int position) {
        return mTypeList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//        if (convertView == null) {
//            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            convertView = inflater.inflate(R.layout.item_type, null);
//        }
//        TextView typename = convertView.findViewById(R.id.type_name);
//        ImageView imageView = convertView.findViewById(R.id.type_image);
//        for(int i = 0;i<mTypeList.size();i++){
//            typename.setText(mTypeList.get(i).getTypeName());
//            imageView.setImageResource(mTypeList.get(i).getsImageId());
//            System.out.println(mTypeList.get(i).toString());
//        }
//        return convertView;
//    }
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_type, null);
        }

        TextView typename = convertView.findViewById(R.id.type_name);
        ImageView imageView = convertView.findViewById(R.id.type_image);

        // 获取当前位置对应的 Type 对象
        Type currentType = mTypeList.get(position);

        // 设置 TextView 和 ImageView 的内容为当前 Type 对象的属性值
        typename.setText(currentType.getTypeName());
        imageView.setImageResource(currentType.getsImageId());

        // 输出当前 Type 对象的信息（可选）
//        System.out.println(currentType.toString());
        if(selectPos == position){
            imageView.setImageResource(currentType.getsImageId());
        }else{
            imageView.setImageResource(currentType.getImageId());
        }
        return convertView;
    }
}
